package org.soarsky.wealthchengdu.google.map;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;




public class LineGoogleOverlays extends Overlay implements LineOverlaysInterface{
	private static final float paintStrokeWidth = 6;
	private List<ArrayList<BasePoint>> lineOverlays = null;
	private PointContorlGoogle pointContorl = null;
	private Point pointNext = null,pointPrev = null;
	private GeoPoint geoPointPrev = null,geoPointNext = null;
	private Projection projection = null;
	private Paint normalPaint  =null,pathEffectPaint = null,circlePaint = null;
	private int pathEffectId = -1;
	
	public LineGoogleOverlays(MapView mapView){
		lineOverlays = new ArrayList<ArrayList<BasePoint>>();
		pointContorl = new PointContorlGoogle();
		
		init();
	} 
	private void init() {
		pointNext= new Point();
		pointPrev = new Point();
		PathEffect mEffects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
		for(int i=0;i<3;i++) {
			Paint p = new Paint();
			p.setAntiAlias(true);
			p.setColor(Color.RED);
			p.setStyle(Paint.Style.FILL_AND_STROKE);
			p.setAlpha(192);
			p.setStrokeWidth(paintStrokeWidth);
			if(i==0){
				normalPaint = p;
			}else if(i==1){
				p.setStrokeWidth(paintStrokeWidth/2f);
				circlePaint = p;
			}else{
				pathEffectPaint = p;
				pathEffectPaint.setPathEffect(mEffects);
				pathEffectPaint.setStrokeWidth(paintStrokeWidth);
			}
		}
		
		
	}
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		//super.draw(canvas, mapView, shadow);
		canvas.save();
		if(null==projection){
			projection = mapView.getProjection();
		}
		int size = lineOverlays.size();
		for(int i=0;i<size;i++){
			int childSize = lineOverlays.get(i).size();
			for(int j=0;j<childSize;j++){
				BasePoint basePoint = lineOverlays.get(i).get(j);
				if(basePoint.getLatitude()==0&& basePoint.getLongitude()==0){
					if(null!=geoPointPrev)
						geoPointPrev = null;
				}else{
					geoPointNext = pointContorl.createGeoPoint(basePoint.getLatitude(), basePoint.getLongitude());
					if(geoPointPrev != null){
						projection.toPixels(geoPointNext, pointNext);
						projection.toPixels(geoPointPrev, pointPrev);
						pointContorl.screenSizeChange(mapView.getWidth(), mapView.getHeight());
						if(pathEffectId==basePoint.getId()&&j==childSize-1){
							canvas.drawLine(pointPrev.x, pointPrev.y, pointNext.x,
									pointNext.y, pathEffectPaint);
						}else
						/*去掉这行有用超出屏幕也显示的情况
						 * if(pointContorl.isOverlap(pointPrev, pointNext)&&(
								pointContorl.isInTheScreen(pointNext)
								||pointContorl.isInTheScreen(pointNext))){
		*/
							canvas.drawLine(pointPrev.x, pointPrev.y, pointNext.x,
									pointNext.y, normalPaint);
							canvas.drawCircle(pointNext.x, pointNext.y, 1f, circlePaint);
						/*}*/
					}else {
						geoPointPrev = geoPointNext;
					}
				}
			
			}
		}
		canvas.restore();
		geoPointPrev=null;
		geoPointNext = null;
	}
	private int indexArray[] = new int[2];
	private int[] getIndex(int id){
		if(null!=lineOverlays){
			int size = lineOverlays.size();
			for(int i=0;i<size;i++){
				int childSize = lineOverlays.get(i).size();
				for(int j=0;j<childSize;j++)
					if(lineOverlays.get(i).get(j).getId()==id){
						indexArray[0]=i;
						indexArray[1]=j;
						return indexArray;
				}
				
			}
		}
		return null;
	}
	public void addLinePoint( BasePoint basePoint) {
		// TODO Auto-generated method stub
		if(null==basePoint||null==lineOverlays)return;
		if(null!=getIndex(basePoint.getId())){
			lineOverlays.get(indexArray[0]).add(basePoint);
		}else {
			createLines(basePoint);
		}
	}  

	public void addLinePoint(BasePoint basePoint, boolean isPathEffect) {
		// TODO Auto-generated method stub
		if(null==basePoint||null==lineOverlays)return;
		if(null!=getIndex(basePoint.getId())){
			if(isPathEffect)
				pathEffectId = basePoint.getId();
			lineOverlays.get(indexArray[0]).add(basePoint);
		}
	}
	
	public void deleteLastLinePoint(int id) {
		// TODO Auto-generated method stub
		if(null!=getIndex(id)){
			pathEffectId=-1;
			lineOverlays.get(indexArray[0]).remove(indexArray[1]);
		}
	}
	public void createLines(BasePoint basePoint) {
		// TODO Auto-generated method stub
		if(null==basePoint||null==lineOverlays)return;
		ArrayList<BasePoint> arrayList = new ArrayList<BasePoint>();
		arrayList.add(basePoint);
		lineOverlays.add(arrayList);
	}
	
	public void deleteLines(int id) {
		// TODO Auto-generated method stub
		if(null==lineOverlays)return;
		if(null!=getIndex(id)){
			lineOverlays.get(indexArray[0]).remove(indexArray[1]);
		}
	}

	public boolean disableLines(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean enableLines(int id) {
		// TODO Auto-generated method stub
		return false;
	}
	public void setLineColor(int color){
		normalPaint.setColor(color);
		pathEffectPaint.setColor(color);
		circlePaint.setColor(color);
	}
	public void setLineColor(int a,int r,int g,int b){
		normalPaint.setARGB(a, r, g, b);
		pathEffectPaint.setARGB(a, r, g, b);
		circlePaint.setARGB(a, r, g, b);
	}
	public void setLineStrokeWidth(float size){
		circlePaint.setStrokeWidth(size/2f);
		normalPaint.setStrokeWidth(size);
		pathEffectPaint.setStrokeWidth(size);
	}

	public void setOnTapCallBack(OverlayCallBack callBack) {
		// TODO Auto-generated method stub
		
	}

	public boolean clearLinePoints() {
		// TODO Auto-generated method stub
		if(null!=lineOverlays){
			lineOverlays.clear();
			return true;
		}
		return false;
	}
	public void distory() {
		// TODO Auto-generated method stub
		if(clearLinePoints()){
			lineOverlays=null;
		}
		if(null!=indexArray){
			indexArray=null;
		}
	}
	
}
